package com.example.demo.server3;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;

import java.net.InetSocketAddress;

public class BaseServer {
    private ServerBootstrap sbs;
    private ChannelFuture future;

    BaseServer(){

    }

    public void start(int port){
        try {
            this.sbs = new ServerBootstrap().
                    group(new NioEventLoopGroup(1),new NioEventLoopGroup()).
                    channel(NioServerSocketChannel.class).
                    localAddress(new InetSocketAddress(port)).
                    //option(ChannelOption.SO_KEEPALIVE, true).//是否启动心跳处理,这里默认两小时才处理,会不会有隐含的问题?
                    //option(ChannelOption.TCP_NODELAY, true).//有可能出现的问题参考https://blog.csdn.net/wdscq1234/article/details/52432095
                    //option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 6 * 1000).//超时机制
                    //option(ChannelOption.AUTO_READ, true).//一个连接的远端关闭时本地端是否关闭，默认值为False。值为False时，连接自动关闭；为True时，触发ChannelInboundHandler的userEventTriggered()方法，事件为ChannelInputShutdownEvent
                    //option(ChannelOption.SO_REUSEADDR, true).//这个参数表示允许重复使用本地地址和端口,这几个option究竟用childoption还是option需要再次思考
                    handler(new ServerCmppHandlePipelineInitializer());

            future = sbs.bind(port).sync();
            System.out.println("Server start listen at " + port);
            future.channel().closeFuture().sync();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        new BaseServer().start(8080);
    }
}
